package com.yc.util;

import com.yc.service.ServiceException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageUtil {
    private Integer pageSize; //页面尺寸
    private Integer pageNum;  //设置查询的页码

    /**
     * 初始化，设置分页尺寸及查询页码
     * @param pageSize
     */
    public PageUtil(int pageNum,int pageSize){
        this.pageSize=pageSize;
        this.pageNum=pageNum;
    }

    public <T> List<T> get(List<T> list) throws ServiceException {
        try {
            checkList(list);
            List<T> newlist=new ArrayList<>();
            int num=pageNum*pageSize>list.size()?list.size():pageNum*pageSize;
            for ( int i = (pageNum-1)*pageSize; i < num; i++ ) {
                newlist.add(list.get(i));
            }
            return newlist;
        } catch ( ServiceException e ) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 检测list是否符合条件
     * @param list
     * @return
     */
    public Boolean checkList(List list) throws ServiceException {
        if ( list==null||list.size()==0 ){
            throw new ServiceException("传入的集合为空，请确定后重试");
        }
        if ( list.size()<(pageNum-1)*pageSize ){
            throw new ServiceException("集合的长度错误，请检查页码和页面尺寸后重新输入");
        }
        return true;
    }

    /**
     * 根据转入的list集合和尺寸进行分页，同时计算总页码，数据的总长度，上上一页，上一页，下一页，下下一页的页码，分页后的数据，并将其存在map集合中返回。
     * @param list :要分类的所有数据集合
     * @param pageSize :每一页的尺寸
     * @param page :要查询的页码
     * @return totalNum：数据总长度；prePage：上一页；preprePage：上上一页；nextPage：下一页；nextnextPage：下下一页；
     * totalPage：总页码；dataList：分页后的数据；curPage：当前页；
     */
    public static Map pageMap(List list,Integer pageSize,Integer page){
        Integer totalPage=totalPage=list.size()/pageSize + (list.size()%pageSize != 0 ? 1 : 0);
        Integer prePage=-1;
        Integer preprePage=-1;
        Integer curPage=-1;
        Integer nextPage=-1;
        Integer nextnextPage=-1;
        Map map=new HashMap();
        map.put("totalNum",list.size());
        if ( list.size()>pageSize ){
            totalPage=list.size()/pageSize + (list.size()%pageSize != 0 ? 1 : 0);
            if ( page==null ){
                PageUtil pageUtil=new PageUtil(1,pageSize);
                try {
                    list=pageUtil.get(list);
                    curPage=1;
                    if ( curPage+1<=totalPage ){
                        nextPage=curPage+1;
                    }
                    if ( curPage+2<=totalPage ){
                        nextnextPage=curPage+2;
                    }
                } catch ( ServiceException e ) {
                    e.printStackTrace();
                }
            }else {
                PageUtil pageUtil=new PageUtil(page,pageSize);
                try {
                    list=pageUtil.get(list);
                    curPage=page;
                    if ( page==1 ){
                        if ( page+1<=totalPage ){
                            nextPage=curPage+1;
                        }
                        if ( page+2<=totalPage ){
                            nextnextPage=curPage+2;
                        }
                    }else if ( page>1 ){
                        if ( page-1>0 ){
                            prePage=curPage-1;
                        }
                        if ( page-2>0 ){
                            preprePage=curPage-2;
                        }
                        if ( page+1<=totalPage ){
                            nextPage=curPage+1;
                        }
                        if ( page+2<=totalPage ){
                            nextnextPage=curPage+2;
                        }
                    }
                } catch ( ServiceException e ) {
                    e.printStackTrace();
                }
            }
        }else {
            curPage=1;
        }
        map.put("prePage",prePage);
        map.put("preprePage",preprePage);
        map.put("curPage",curPage);
        map.put("nextPage",nextPage);
        map.put("nextnextPage",nextnextPage);
        map.put("totalPage",totalPage);
        map.put("dataList",list);
        return map;
    }
}
